<template>
  <div class="singer" ref="singer">
    <list-view @select="selectSinger" :data="singers" ref="list"></list-view>
    <router-view></router-view>
  </div>
</template>

<script>
  import {getSingerList} from 'api/singer'
  import {ERR_OK} from 'api/config'
  import Singer from 'common/js/singer'
  import ListView from 'base/listview/listview'
  import {mapMutations} from 'vuex'
  import {playlistMixin} from 'common/js/mixin'

  const HOT_NAME = '热门'
  const HOT_SINGER_LENGTH = 10

  export default {
    mixins: [playlistMixin],                        // 组件插入mixin。相当于把mixin里面的代码copy到这个组件里面。组件里面的同名的方法会覆盖mixin里面的方法
    data () {
      return {
        singers: []
      }
    },
    components: {
      ListView
    },
    mounted () {
      this._getSingerList()
    },
    methods: {
      ...mapMutations({                                // 关联store/mutation-types.js中常量  做了一些映射
        setSinger: 'SET_SINGER'
      }),
      selectSinger (singer) {                          // 跳转路由。singer的2级子路由
        this.$router.push({
          path: `/singer/${singer.id}`
        })
        this.setSinger(singer)                         // vuex 的方法封装setSinger    这里就是往vuex state里面存了一些数据，在singer-detail.vue中会取这些数据
      },
      _getSingerList () {
        getSingerList().then((res) => {
          if (res.code === ERR_OK) {
            this.singers = this._normalizeSinger(res.data.list)
          }
        })
      },
      _normalizeSinger (list) {
        let map = {
          hot: {
            title: HOT_NAME,
            items: []
          }
        }
        list.forEach((item, index) => {
          if (index < HOT_SINGER_LENGTH) {
            map.hot.items.push(new Singer({
              id: item.Fsinger_mid,
              name: item.Fsinger_name
            }))
          }
          const key = item.Findex
          if (!map[key]) {                            // map里面没有这个key值存在
            map[key] = {
              title: key,
              items: []
            }
          }
          map[key].items.push(new Singer({
            id: item.Fsinger_mid,
            name: item.Fsinger_name
          }))
        })
        // 为了得到有序列表，我们需要处理map
        let hot = []
        let ret = []
        for (let key in map) {
          let val = map[key]
          if (val.title.match(/[a-zA-Z]/)) {          // 正则验证是否是字母
            ret.push(val)
          } else if (val.title === HOT_NAME) {
            hot.push(val)
          }
        }
        // 数组排序sort()
        ret.sort((a, b) => {
          return a.title.charCodeAt(0) - b.title.charCodeAt(0)              // 结果大于0，那么就是return true。否则就是返回结果
        })
        return hot.concat(ret)                             // 数组拼接
      },
      handlePlaylist (playList) {    // mixin必须的函数
        const bottom = playList.length > 0 ? '60px' : ''
        this.$refs.singer.style.bottom = bottom                     // 这里是组件上的ref。所以是vue对象。需要使用.$el
        this.$refs.list.refresh()
      }
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.singer{
	position:fixed;
	top:88px;
	bottom:0;
	width:100%;
}
</style>
